﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for MethodClassCommon
/// </summary>
public class MethodClassCommon
{
    SqlConnection con;
    SqlCommand command;
    SqlDataReader dr;
    Connection dt;
    SqlDataAdapter da;
    DataSet ds;
	public MethodClassCommon()
	{
        dt = new Connection();
        con = dt.getConnect();
	}
    int execute()
    {
        SqlParameter res = new SqlParameter("@res", SqlDbType.Int);
        res.Direction = ParameterDirection.ReturnValue;
        command.Parameters.Add(res);
        command.ExecuteNonQuery();
        con.Close();
        int kq = Convert.ToInt32(res.Value);
        return kq;
    }
    public int Update_Data(string strSQL, CommandType ct, ref string error, params SqlParameter[] param)
    {
       
            using (SqlCommand Comm = new SqlCommand(strSQL,con))
            {
                int kq = 1;
                Comm.Parameters.Clear();
                Comm.CommandText = strSQL;
                Comm.CommandType = ct;
                Array.ForEach(param, paramItem => Comm.Parameters.Add(paramItem));
                try
                {
                    con.Open();
                    SqlParameter res = new SqlParameter("@res", SqlDbType.Int);
                    res.Direction = ParameterDirection.ReturnValue;
                    Comm.Parameters.Add(res);
                    Comm.ExecuteNonQuery();
                     kq = Convert.ToInt32(res.Value);
                    
                }
                catch (SqlException ex)
                {
                    error = ex.Message;
                }
                finally
                {
                    con.Close();
                }
                return kq;
            }
        
    }
    public int deleteData(string procedureName, string vPrimaryKey,string parameter)
    {
        con.Open();
        command = new SqlCommand(procedureName, con);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@" + vPrimaryKey, parameter));
        int kq = execute();
        con.Close();
        return kq;
    }
    public string GetNewKeySale(string ProcedureName)
    {
        con.Open();
        string Ma = "";
       
        command = new SqlCommand(ProcedureName, con);
        command.CommandType = CommandType.StoredProcedure;
        dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        if (dr.HasRows.Equals(true))
        {
            Ma =Convert.ToString(Convert.ToDouble( dr[0].ToString()) + 1);

        }
        else
        {
            Ma = "1";
        }
        dr.Close();
        return Ma;
    }
    public string GetFirstOrLastData(string ProcedureName)
    {
        con.Open();
        string Ma = "";
        string MoTa = "";
        command = new SqlCommand(ProcedureName,con);
        command.CommandType = CommandType.StoredProcedure;
        dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        if (dr.HasRows.Equals(true))
        {
            Ma = dr[0].ToString();
            MoTa = dr[1].ToString();
        }
        dr.Close();
        return Ma + "|" + MoTa;
    }
    public string GetNext(string ProcedureName,string parameter)
    {
        string Ma = "";
        string MoTa = "";
        con.Open();
        da = new SqlDataAdapter(ProcedureName, con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        ds = new DataSet();
        da.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            if (parameter == ds.Tables[0].Rows[i][0].ToString())
            {
                Ma = ds.Tables[0].Rows[i + 1][0].ToString();
                MoTa = ds.Tables[0].Rows[i + 1][1].ToString();
                break;
            }

        }

        con.Close();
        return Ma + "|" + MoTa;
    }
    public string GetDataDepend(string pro, string vKey, string key)
    {
        con.Open();
        command = new SqlCommand(pro, con);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@" + vKey, key));
        dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        string data = dr[0].ToString();
        dr.Close();
        return data;
    }
    public string GetDataDepend(string pro, string vKey1,string  key1, string vKey2,string key2, string vKey3, string key3, string vKey4,string  key4)
    {
        con.Open();
        command = new SqlCommand(pro, con);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@" + vKey1, key1));
        command.Parameters.Add(new SqlParameter("@" + vKey2, key2));
        command.Parameters.Add(new SqlParameter("@" + vKey3, key3));
        command.Parameters.Add(new SqlParameter("@" + vKey4, key4));
        dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        string data = dr[0].ToString();
        dr.Close();
        return data;
    }
   
    public string GetDataDependCayPhanCap(string pro, string vKey1, string key1,string vKey2, string key2)
    {
        con.Open();
        command = new SqlCommand(pro, con);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@" + vKey1, key1));
        command.Parameters.Add(new SqlParameter("@" + vKey2, key2));
        dr = command.ExecuteReader(CommandBehavior.CloseConnection);
        dr.Read();
        string data = dr[0].ToString();
        dr.Close();
        return data;
    }
    public string GetPrevious(string ProcedureName ,string parameter)
    {
        
        string Ma = "";
        string MoTa = "";
        con.Open();
        da = new SqlDataAdapter(ProcedureName,con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        ds = new DataSet();
        da.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            
            if (parameter == ds.Tables[0].Rows[i][0].ToString())
            {
                Ma = ds.Tables[0].Rows[i - 1][0].ToString();
                MoTa = ds.Tables[0].Rows[i - 1][1].ToString();
                break;
            }
           
        }
       
        con.Close();
        return Ma + "|" + MoTa;
       
    }


}
